package com.group.library.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.library.order.dao.NoticeMapper;
import com.group.library.order.dao.SeatMapper;
import com.group.library.order.dao.UserBaseMapper;
import com.group.library.order.entity.Notice;
import com.group.library.order.entity.Seat;
import com.group.library.order.entity.User;
import com.group.library.order.entity.UserLogin;
import com.group.library.order.service.UserBaseService;
import com.group.library.order.util.RandomUtil;
import com.group.library.order.util.SHA256Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import java.util.List;

@Service
public class UserBaseServiceImpl implements UserBaseService {


    @Autowired
    private UserBaseMapper userBaseMapper;

    /*
        从小程序出发
        用户查看自己信息，或是需要用户信息时调用
     */
    @Autowired
    private SeatMapper seatMapper;

    @Autowired
    private NoticeMapper noticeMapper;



    @Override
    public User getUserInfo() {
        return userBaseMapper.findUserInfo();
    }

    /*
        管理员查看用户信息
     */
    @Override
    public PageInfo<User> listUser(Integer page, Integer limit, String name, String phone, String number) {
        PageHelper.startPage(page,limit);
        List<User> users = userBaseMapper.listUser(name,phone,number);
        System.out.println("users-->"+users);
        return new PageInfo<User>(users);
    }

    /*
        添加用户测试
     */

    @Override
    public boolean addUser() {
        System.out.println("<------测试------>");
        User user = new User();
        user.setUserId(RandomUtil.getUUID());
        user.setName("廖锦辉");
        user.setSex(1);
        user.setPhone("18475339252");
        user.setNumber("1914080902512");
        user.setUniversity("惠州学院");
        user.setFaculty("计算机科学与工程学院");
        user.setMajor("软件工程");
        user.setGrade("大三");
        user.setIconPath("http://rb728hhl7.hn-bkt.clouddn.com/user0.jpeg");
        user.setEntranceYear("2019-09");
        user.setGraduateYear("2023-06");
        user.setCredit(100);
        user.setToOrder(true);
        user.setLateCount(0);
        user.setAbsentCount(0);
        user.setLeaveEarlyCount(0);
        user.setCancelCount(0);
        return userBaseMapper.addUser(user)==1;
    }

    //验证用户信息
    @Override
    public UserLogin ExitUser(String number, String password) {

        if(number.equals(userBaseMapper.findBaseInfo().getNumber())){
            if(SHA256Util.getSHA256(password).equals(userBaseMapper.findBaseInfo().getPassword())){
                return userBaseMapper.findBaseInfo();
            }
        }
        return null;
    }

    //返回用户详细信息
    @Override
    public User showInfo() {
        return userBaseMapper.findUserInfo();
    }

    //修改用户信息
    @Override
    public User modifyUser(String userId, String name, String number, String faculty, String grade, String phone) {

            userBaseMapper.modifyUser(userId, name, number, faculty, grade, phone);
            return userBaseMapper.findUserInfo();

    }

    //验证用户修改密码
//    @Override
//    public boolean modifyResult(String newPwd,String userNum) {
//
//        if(!SHA256Util.getSHA256(newPwd).equals(userBaseMapper.findBaseInfo().getPassword())){
//            userBaseMapper.updateUser(userNum,SHA256Util.getSHA256(newPwd));
//            System.out.println(userNum);
//            System.out.println(SHA256Util.getSHA256(newPwd));
//            return true;
//        }
//        return false;
//    }


    //返回座位详细信息
    @Override
    public ArrayList<HashMap<Integer, ArrayList<HashMap<Integer,String>>>> getSeatInfo() {
        //楼层与座位的集合
        HashMap<Integer, ArrayList<HashMap<Integer,String>>> floorAndLocation = new HashMap<>();

        ArrayList<HashMap<Integer, ArrayList<HashMap<Integer,String>>>> result =new ArrayList<>();

        HashSet<Integer> floors = new HashSet<>();
        //找出共有几层楼

            final ArrayList<Seat> seatInfo = seatMapper.getSeatInfo();
            //获取楼层数
            for (Seat seat : seatInfo) {
                floors.add(seat.getFloor());
            }

            //根据楼层获取座位详细信息
            for (Integer floor : floors) {

                ArrayList<HashMap<Integer,String>> alInfo = new ArrayList<>();
                //根据每层楼的座位状态获取座位数组
                for (Seat seat : seatInfo) {

                    if(seat.getFloor()==floor) {
                        //状态与座位的信息
                        HashMap<Integer ,String> statusAndLocation = new HashMap<>();
                        statusAndLocation.put(seat.getStatus(),seat.getLocation());
                        alInfo.add(statusAndLocation);
                    }

                    floorAndLocation.put(floor,alInfo);
                }
                }
            result.add(floorAndLocation);
            return result;
    }

    //获取公告信息
    @Override
    public Notice getNoticeInfo() {
        return noticeMapper.getNoticeInfo();
    }

}
